// pages/zshApp/zshApp.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    time: "",
    dateI: "",
    dateP: "",
    departmentI: "",
    departmentP: "",
    departmentList: "",
    currentDate: "",
    docList: [],
    isInit: true,
    date: new Date().getTime(),
    isTimeShow: false,
    isDepartShow: false,
    currentDate: new Date().getTime(),
    minDate: new Date().getTime(),
    maxDate: new Date(2022,4,10).getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      if (type === 'day') {
        return `${value}日`;
      }
      if (type === 'hour') {
        return `${value}小时`;
      }
      if (type === 'minute') {
        return ``;
      }
      return value;
    },
  },


  selectTime() {
    // console.log(this.data.date)
    if(this.data.isInit) {
      let t = new Date().getTime()
      let tt = new Date(t)
      let yyy = tt.getFullYear()
      let mmm = tt.getMonth()
      let ddd = tt.getDate() + 4
      this.setData({
        maxDate : new Date(yyy,mmm,ddd).getTime()
      })
      // console.log(t.getFullYear())
      // console.log(t.getMonth() + 1)
      // console.log(t.getHours())
    }
    this.setData({
      isTimeShow: true,
      isDepartShow : false
    })
  },

  selectDepart() {
    this.setData({
      isDepartShow : true,
      isTimeShow: false
    })
  },

/**
   * 对传入的时间值进行格式化。后台传入前台的时间有两种个是：Sql时间和.Net时间
   * @param {string|Date} sValue 传入的时间字符串
   * @param {string|boolean} dateFormat  日期格式，日期格式：eg：'Y-m-d H:i:s'
   * @return {string} 2014-03-01 这种格式
   * @example
   * 1) Sql时间格式：2015-02-24T00:00:00
   * 2) .Net时间格式：/Date(1410744626000)/
   */
  getDateTimeStr: function (sValue, dateFormat = 'Y-m-d') {
    var dt;
    // 1.先解析传入的时间对象，
    if (sValue) {
      // 不为Date格式，就转换为DateTime类型
      if (toString.call(sValue) !== '[object Date]') {
        // 1)纯数字
        if (toString.call(sValue) === '[object Number]') {
          dt = new Date(sValue);
        } else {
          // 2.字符串
          sValue = sValue + '';
          sValue = sValue.replace(/-/g, '/');
          if (sValue.indexOf('T') > 0) {
            // 1)格式：2015-02-24T00:00:00
            var timestr = sValue.replace('T', ' ').replace(/-/g, '/'); //=> 2015/02/24 00:00:00
            dt = new Date(timestr);
          } else if (sValue.indexOf('Date') >= 0) {
            // 2).Net格式：/Date(1410744626000)/
            //Convert date type that .NET can bind to DateTime
            //var date = new Date(parseInt(sValue.substr(6)));
            var timestr = sValue.toString().replace(/\/Date\((\d+)\)\//gi, '$1'); //
            dt = new Date(Math.abs(timestr));
          } else {
            dt = new Date(sValue);
          }
        }
      } else {
        dt = sValue;
      }
    } else {
      return '';
    }

    // 2.转换
    // 1)转换成对象 'Y-m-d H:i:s'
    var obj = {}; //返回的对象，包含了 year(年)、month(月)、day(日)
    obj.Y = dt.getFullYear(); //年
    obj.m = dt.getMonth() + 1; //月
    obj.d = dt.getDate(); //日期
    obj.H = dt.getHours();
    // obj.i = dt.getMinutes();
    // obj.s = dt.getSeconds();
    obj.i = 0;
    obj.s = 0;
    //2.2单位的月、日都转换成双位
    if (obj.m < 10) {
      obj.m = '0' + obj.m;
    }
    if (obj.d < 10) {
      obj.d = '0' + obj.d;
    }
    if (obj.H < 10) {
      obj.H = '0' + obj.H;
    }
    if (obj.i < 10) {
      obj.i = '0' + obj.i;
    }
    if (obj.s < 10) {
      obj.s = '0' + obj.s;
    }
    // 3.解析
    var rs = dateFormat.replace('Y', obj.Y).replace('m', obj.m).replace('d', obj.d).replace('H', obj.H).replace('i', obj.i).replace('s', 0);
    return rs;
  },

  onTimeConfirm(aaa) {
    console.log(aaa.detail)
    // console.log(this.data.date)
    let time = new Date(aaa.detail)
    let year = time.getFullYear()
    let month = time.getMonth() + 1
    let day = time.getDate()
    let hour = time.getHours()
    let ymdInput = `${year}年${month}月${day}日${hour}小时`
    this.setData({
      dateI: ymdInput,
      dateP: time
    })
    console.log(this.data.dateP)
    console.log(this.getDateTimeStr(this.data.dateP,'Y-m-d H:i:s'))
    let params = {
      departmentId: this.data.departmentP,
      time: this.getDateTimeStr(this.data.dateP,'Y-m-d H:i:s')
    }
    console.log(params)
    wx.request({
      url: 'https://118.31.170.229:5001/api/ZshStock/GetStockBriefs',
      method: 'get',
      data: params,
      // data: {
      //   // departmentId: this.data.departmentP,
      //   // time: this.data.dateP
      //   departmentId: this.data.departmentP,
      //   time: this.data.dateP
      // },
      success: (res) => {
        console.log(res)
        this.setData({
          isTimeShow: false,
        })
      }
      
    })
  },

  onDepartConfirm(val) {
    console.log(val.detail.value)
    this.setData({
      isDepartShow: false,
      departmentI: val.detail.value.name,
      departmentP: val.detail.value.id
    })
    let params = {
      departmentId: this.data.departmentP,
      time: this.getDateTimeStr(this.data.dateP,'Y-m-d H:i:s')
    }
    console.log(params)
    wx.request({
      url: 'https://118.31.170.229:5001/api/ZshStock/GetStockBriefs',
      method: 'get',
      data: params,
      success: (res) => {
        console.log(res)
        this.setData({
          docList : res.data
        })
        console.log((new Date(this.data.docList[0].time).getHours())+1)

      }
    })
  },

  onTimeCancel() {
    this.setData({
      isTimeShow: false
    })
  },

  onDepartCancel() {
    this.setData({
      isDepartShow: false
    })
  },

  onTimeChange(val) {
    console.log(this.data.date)
  },

  departmentChange(val) {
    console.log(val)
  },

  detail(data) {
    console.log(data.target.dataset.id)
    let id = data.target.dataset.id
    let userInfo = wx.getStorageSync('userInfo')
    console.log(userInfo)
    wx.navigateTo({
      url: '../zshAppDetail/zshAppDetail?id='+id+'&userId='+userInfo.id,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.request({
      url: 'https://118.31.170.229:5001/api/ZshDepartment/GetDepartments',
      methodL: 'get',
      success: (res) => {
        // console.log(res)
        this.setData({
          departmentList : res.data
        })
        console.log(this.data.departmentList)
        console.log()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})